package com.lanou3g.k12.web.controller;

import com.lanou3g.k12.entity.Event;
import com.lanou3g.k12.entity.EventContent;
import com.lanou3g.k12.entity.Notice;
import com.lanou3g.k12.service.CourseService;
import com.lanou3g.k12.entity.EventContent;
import com.lanou3g.k12.service.EventService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class EventController {
    @Autowired
    private EventService eventService;

    //  添加新闻以及新闻内容
    @PostMapping("/event")
    public K12Result addEvent(String title, Long cid, String content, Integer status) {
        try {
            eventService.addEvent(title, cid, status, content);
            System.out.println(title);
            System.out.println(content);
            System.out.println(cid);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }

    //    根据id删除新闻以及新闻内容
    @DeleteMapping("/event/{id}")
    public K12Result deleteEventById(@PathVariable Long id) {
        try {
            eventService.deleteEventById(id);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }

    //    根据ids批量删除新闻以及新闻内容
    @DeleteMapping("/events/{ids}")
    public K12Result deleteEventsByIds(@PathVariable Long[] ids) {
        eventService.deleteEventsByIds(ids);
        return K12ResultUtils.ok();
    }

    //    根据keyword查询活动列表
    @GetMapping("/events")
    public K12Result findAllEvent(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", defaultValue = "30") Integer size,
                                  String keyword) {
        try {
            List<Event> eventList;
            if (keyword != null && !keyword.trim().equals("")) {
                eventList = eventService.findEventByKeyword(page, size, keyword);
            } else {
                eventList = eventService.findAllEvent(page, size);
            }
            return K12ResultUtils.ok(eventList);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

    //     根据id查找标题下的内容
    @GetMapping("/events/{id}")
    public K12Result findContentById(@PathVariable Long id) {
        try {
            List<EventContent> eventContents = eventService.findContentById(id);
            return K12ResultUtils.ok(eventContents);
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }


    //    对活动列表的禁用
    @PutMapping("/event/{id}")
    public K12Result updateStatus(@PathVariable Long id) {
        try {
            eventService.updateStatus(id);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }

    //    根据id查询到活动内容
    @GetMapping("/eventContent/{id}")
    public K12Result findEventById(@PathVariable Long id) {
        Event event = eventService.findEventContentById(id);
        return K12ResultUtils.ok(event);
    }

    //    根据id修改content
    @PutMapping("/eventContent/{id}")
    public K12Result updateById(@PathVariable Long id, EventContent eventContent) {
        try {
            eventService.updateEventContentById(eventContent);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }


}















